// pages/food1/food1.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    access_token:'',
    dish:'',
    tempFilePaths:'',
    base64:'',

// 卡片
contacts: [{
  id: 1,
  name: '结果2',
  phone: '0101 123456',
  mobile: '0770 123456',
  email: 'frank@emailionicsorter.com'
},
{
  id: 2,
  name: '结果3',
  phone: '0101 123456',
  mobile: '0770 123456',
  email: 'frank@emailionicsorter.com'
},
],
contact: {},
transformIdx: 0,
position: 'center',
duration: 300,
show: false,
overlay: false
   
    // contacts: [
    //   {
    //     id: 1,
    //     name:"结果2",
    //     // things:that.dish[1].name
    //     // probability:"{{dish[1].probability}}",
    //     // description:"{{dish[1].baike_info.description}}",
    //   },
    //   {
    //   id: 2,
    //   name:"结果3",
    //   // things:that.dish[2].name
    //   // probability:"{{dish[2].probability}}",
    //   // description:"{{dish[2].baike_info.description}}",
    // },]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    var tempFilePaths = tempFilePaths
    var base64 = base64
    // console.log("temp",options.tempFilePaths)
    // console.log("temp2",options.base64)
    that.setData({
      tempFilePaths: options.tempFilePaths,
      base64:options.base64
    })
    // 1. 获取access_token
    wx.request({
      url: 'https://aip.baidubce.com/oauth/2.0/token?', //仅为示例，并非真实的接口地址
      data: {
        grant_type:'client_credentials',
        client_id: 'hdnxIBRBgnMwQUME0TdN9GLf',
        client_secret:'WAcpk4yKqzY4sOA4vcMsa8hW40inMVg6'
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        that.data.access_token = res.data.access_token
        var base64 = options.base64
        // console.log("查看tempFilePaths情况",base64)
        // console.log ('access_token',that.data.access_token)
        that.tupian_request(that.data.access_token,base64)
      }
    })
 },



  tupian_request(access_token,base64){
    var that = this
    // console.log("base64",base64),
    wx.request({
      url: 'https://aip.baidubce.com/rest/2.0/image-classify/v2/dish', //仅为示例，并非真实的接口地址
      
      data: {
        access_token: access_token,
        image: base64,
        baike_num: 3
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded' // 默认值
      },
      method:'POST',
      success (res) {
        console.log("查看res.data信息",res.data)
        that.setData({
          dish:res.data.result,
          // dish_name:res.data.result[0].name,
          // description:res.data.result[0].baike_info.description,
          // dish_name1:res.data.result[1].name,
          // dish_name2:res.data.result[2].name,
          // probability:res.data.result[0].probability
        })
        //  console.log("res.data",res.data)    
      }
    })
  },

// 卡片
  isCard(e) {
    this.setData({
      isCard: e.detail.value
    })
  },

  showNext(e) {
    var that=this;
    var idx = e.currentTarget.dataset.idx;
    // console.log("Chakanidx",e.currentTarget)
    this.setData({
      show: true,
      contact: this.data.contacts[idx],
      transformIdx: idx
    })
  },

  showPrev() {
    var that=this;
    this.setData({
      show: false
    })
  },

  onBeforeEnter(res) {
    // console.log(res)
  },
  onEnter(res) {
    // console.log(res)
  },
  onAfterEnter(res) {
    // console.log(res)
  },
  onBeforeLeave(res) {
    // console.log(res)
  },
  onLeave(res) {
    // console.log(res)
  },
  onAfterLeave(res) {
    // console.log(res)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }

  })